home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / GOFER / scripts / SternBroc < prev   
Text File  |  1993-11-24  |  930b  |  28 lines

  1. -- Produce the stream of closest rational approximations to a
  2. -- floating point number for a given size of denominator.
  3.  
  4. fracts :: Float -> [Rational]
  5.  
  6. type Op = ((Int,Int),(Int,Int)) -> ((Int,Int),(Int,Int)) in
  7.      left, right, move, sternBrocRep
  8.  
  9. left, right :: Op
  10. left = \ ((m,n),(m',n')) -> ((m,n),(m+m',n+n'))
  11. right =  \ ((m,n),(m',n')) -> ((m+m',n+n'),(m',n'))
  12.  
  13. move :: [Op] -> ((Int,Int),(Int,Int)) -> ((Int,Int),(Int,Int))
  14. move  []   y   =  y
  15. move  (x:xs) y = (move xs) (x y)
  16.  
  17. sternBrocRep :: Float -> [Op]
  18. sternBrocRep x | x == 1.0 = []
  19. sternBrocRep x | x > 1.0  = right:(sternBrocRep (x-1.0))
  20. sternBrocRep x | x < 1.0  = left:(sternBrocRep (x/(1.0-x)))
  21.  
  22. fracts x = [ z | sign = signum x, 
  23.                  xs = sternBrocRep (abs x),
  24.                  k <- [1..],
  25.                  ((m,n),(m',n')) = move (take k xs) ((0,1),(1,0)),
  26.                  z = sign*((m+m'):/(n+n')),
  27.                  abs (x-z*1.0) < 0.5 ]
  28.